// pages/myaudio/audio.js
Page({
    onLoad: function (e) {
        console.log(e)
        let id = e.id
        let name = e.name
        let mydir = e.mydir
        const innerAudioContext = wx.createInnerAudioContext({
            useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
        })
        this.setData({
            myplayer: innerAudioContext,
            name: name,
            src: 'https://www.syrkitty.com/music/audio/' + mydir + "/" + id + ".mp3",
            srcText: 'https://www.syrkitty.com/music/text/' + mydir + "/" + id + '.txt',
        })

    },
    onReady: function (e) {

    },
    onUnload: function (e) {
        console.log("onUnload..")
        this.data.myplayer.pause(0)
    },
    data: {
        myplayer: null,
        poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
        name: '此时此刻',
        author: '许巍',
        src: 'https://www.syrkitty.com/music/audio/11.mp3',
        srcText: 'https://www.syrkitty.com/music/text/99.txt',
        content: '原文',
        times:0
    },
    audioPlay: function () {
        // console.log(this.data.myplayer)
        this.data.myplayer.src = this.data.src
        this.data.src = this.data.src
        this.data.myplayer.play() // 播放
    },
    audioPlayTimes:function(e){
        let times = e.target.dataset.times
        this.data.times = times
        wx.getBackgroundAudioManager().title = this.data.name
        wx.getBackgroundAudioManager().src = this.data.src

        wx.getBackgroundAudioManager().onEnded(()=>{
            // console.log("getBackgroundAudioManager................")
            if(this.data.times > 1){
                wx.getBackgroundAudioManager().title = this.data.name
                wx.getBackgroundAudioManager().src = this.data.src
            }
            this.setData({
                times:this.data.times-1
            })
           
        })
    },
    audioPause: function () {
        this.data.myplayer.pause()
    },

    audioStart: function () {
        this.data.myplayer.seek(0)
    },
    hideText:function(){
        this.setData({
            content:''
        })
    },
    showText: function () {

        //需要处理缓存：https://blog.csdn.net/guairena/article/details/118682121
        // wx.downloadFile({
        //     // 示例 url，并非真实存在
        //     url: 'https://www.syrkitty.com/music/text/2/2.doc',
        //     success: function (res) {
        //         const filePath = res.tempFilePath
        //         wx.openDocument({
        //             filePath: filePath,
        //             success: function (res) {
        //                 console.log('打开文档成功')
        //             }
        //         })
        //     }
        // })

        let that = this
        wx.request({
          url: this.data.srcText,
          success:function(res){
            //   console.log(res)
              that.setData({
                  content:res.data
              })
          }
        })


    }
})